package com.vsii.sample;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import com.vsii.sample.data.Employee;
import com.vsii.sample.data.People;
import com.vsii.sample.data.Student;

public class ReadCSV {
	private Scanner scn;
	List<Student> st = new ArrayList<Student>();
	List<Employee> ee = new ArrayList<Employee>();
	List<People> pp = new ArrayList<People>();
	Student s ;
	Employee e ;
	public void readFile(String fileName) {
		File f = new File(fileName);
        	
		try {
			scn = new Scanner(f);

			while (scn.hasNext()) {
				String[] line = scn.nextLine().split(",");
				int number = line.length;
				// What is 21 ?
				for (int i = 0; i < number; i++) {
					for (int j = 0; j < 21; j++)
						System.out.print("-");
				}
				System.out.println();
				for (int i = 0; i < number; i++)
					System.out.format("|%20s", line[i]);
				System.out.println("|");

				if (line[1].equals("SV")) {
					s = addLineS(line);
					s.setScore(Integer.parseInt(line[number - 2]));
					st.add(s);
					pp.add(s);
				}	
				else {
					e = addLineE(line);					e.setTitle(line[number-1]);
					ee.add(e);
					pp.add(e);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	public void write(String f) throws IOException{
		 FileWriter f1 = new FileWriter(f);
	        PrintWriter out = new PrintWriter(f1);
	        for(int i = 0; i < st.size(); i++){
	        	 out.print(st.get(i).getId() + "," + st.get(i).getInfoType()+ "," +st.get(i).getName()+ "," +st.get(i).getAge()+ "," +st.get(i).getAddress()+ "," +st.get(i).getMobile()+ ","+st.get(i).getScore());
			     out.println(","); 
	        }
	        for(int i = 0; i < ee.size(); i++){
		        out.print(ee.get(i).getId() + "," + ee.get(i).getInfoType()+ "," +ee.get(i).getName()+ "," +ee.get(i).getAge()+ "," +ee.get(i).getAddress()+ "," +ee.get(i).getMobile()+ "," + " "+ ","+ee.get(i).getTitle());
		        out.println(",");   
	        }
	        if(out != null){  
		        out.close();
	        }
		
	}
	public Student addLineS(String[] line) {
		// Why do you use this constructor when you have constructor with params ?
		Student student = new Student(Integer.parseInt(line[0]),line[1],line[2],Integer.parseInt(line[4]),line[5],Integer.parseInt(line[6]));
		return student;
	}

	public Employee addLineE(String[] line) {
		Employee employee = new Employee(Integer.parseInt(line[0]),line[1],line[2],Integer.parseInt(line[4]),line[5],Integer.parseInt(line[6]));
		return employee;
	}
	// Sap xep theo id
	public static Comparator<Employee> id = new Comparator<Employee>() {


        public int compare(Employee e1, Employee e2) {
            return e1.getId()- e2.getId();
        }
   };   
   	public boolean check(String s){
   		try{
   			int x=  Integer.valueOf(s.trim());
   				return true;
		}
   		catch(Exception e4x){
			return false;
		}
	}

	public boolean isNumber(String str) {
		for (char c : str.toCharArray()) {
			if (!Character.isDigit(c)) {
				return false;
			}
		}
		return true;
	}

	public void print() {
		System.out.println("Bang sinh vien");
		for (int i = 0; i < st.size(); i++) {
			Collections.sort(st);
			System.out.println(st.get(i).toString());
		}
		System.out.println("Bang nhan vien");
		for (int i = 0; i < ee.size(); i++) {
			Collections.sort(ee);
			System.out.println(ee.get(i).toString());
		}

		System.out.println("Bang danh sanh nhung nguoi vua la nhan vien vua la sinh vien");
		for (int i = 0; i < st.size(); i++) {
			for (int j = 0; j < ee.size(); j++) {
				if (st.get(i).getId() == (ee.get(j).getId())
						&& st.get(i).getName().equals(ee.get(j).getName())) {
					System.out.println(st.get(i).toString());
					System.out.println(ee.get(j).toString());
				}
			}
		}

		System.out.println("Bang du lieu loi ");
		for (int i = 0; i < pp.size(); i++) {
			if ((!check(pp.get(i).getId() + " "))
					|| (!check(pp.get(i).getAge() + " ")))
				System.out.println(pp.get(i).toString());
		}

		//javarevisited.blogspot.com/2012/12/how-to-read-input-from-command-line-in-java.html#ixzz3F4dUdN2P
	}
	// if(!isNumber(st.get(i).getScore())){
	// System.out.println(pp.get(i).toString());
	// }

	// Sap xep theo title
	// public static Comparator<Employee> title = new Comparator<Employee>() {
	// public int compare(Employee e3, Employee e4) {
	// return e3.getTitle().compareTo(e4.getTitle());
	// }
	// };
	public boolean ChangeType(int id, String type, String name, int age, String address, int moblie){
		for (People p : pp) 
			if(p.getId() == id){
					p.setInfoType(type);
					p.setName(name);
					p.setAge(age);
					p.setAddress(address);
					p.setMobile(moblie);
					return true;
			}
		return false;
	}
	
}
